﻿Imports System.Collections.Generic
Imports System.Data
Imports System.Data.SqlClient


''' <summary>
''' Clase de la capa de acceso a datos DAL que implementa el patrón Table Data Gateway para el manejo de la entidad Backup
''' </summary>
''' <remarks></remarks>
Public Class TDGBackup

    Private Shared m_Instance As TDGBackup = Nothing
    Private Shared m_Semaforo As New Object


    Public Function doBackup(conn As SqlConnection, Bkp_Descripcion As String, Bkp_Path As String, Usu_Legajo As Int16) As Int16
        Dim command As New SqlCommand
        Dim iRowsAffected As Int16


        command.Connection = conn
        command.CommandType = CommandType.StoredProcedure

        command.CommandText = "dbo.doBackup"
        command.Parameters.AddWithValue("@Bkp_Descripcion", Bkp_Descripcion)
        command.Parameters.AddWithValue("@Bkp_Path", Bkp_Path)
        command.Parameters.AddWithValue("@Usu_Legajo", Usu_Legajo)


        iRowsAffected = command.ExecuteNonQuery()


        Return iRowsAffected

    End Function

    Public Function doRestore(conn As SqlConnection, Bkp_Path As String) As Int16
        Dim command As New SqlCommand
        Dim iRowsAffected As Int16

        ' Cambio a master
        command.Connection = conn
        command.CommandType = CommandType.Text
        command.CommandText = "USE master;"
        command.ExecuteNonQuery()

        ' Ejecuto restore
        command.Connection = conn
        command.CommandType = CommandType.StoredProcedure
        command.CommandText = "dbo.doRestore"
        command.Parameters.AddWithValue("@Bkp_Path", Bkp_Path)
        iRowsAffected = command.ExecuteNonQuery()

        ' Cambio a master
        command.Connection = conn
        command.CommandType = CommandType.Text
        command.CommandText = "USE Recruiting;"
        command.ExecuteNonQuery()

        Return iRowsAffected

    End Function

    Public Function Update_InactivarBackup(conn As SqlConnection, Bkp_Id As Int16) As Int16
        Dim command As New SqlCommand
        Dim iRowsAffected As Int16


        command.Connection = conn
        command.CommandType = CommandType.StoredProcedure

        command.CommandText = "dbo.updBackup_Inactivar"
        command.Parameters.AddWithValue("@Bkp_Id", Bkp_Id)


        iRowsAffected = command.ExecuteNonQuery()

        Return iRowsAffected

    End Function

    Public Function FindAll(conn As SqlConnection) As SqlDataReader
        Dim command As New SqlCommand
        Dim dr As SqlDataReader

        command.Connection = conn
        command.CommandType = CommandType.StoredProcedure

        command.CommandText = "dbo.getBackupDB_All"


        dr = command.ExecuteReader()

        Return dr

    End Function

    Public Shared Function Instance()
        If m_Instance Is Nothing Then
            SyncLock (m_Semaforo)
                If m_Instance Is Nothing Then
                    m_Instance = New TDGBackup
                End If
            End SyncLock
        End If

        Return m_Instance
    End Function

    Private Sub New()

    End Sub

End Class
